<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>菜单管理</title>
    <link rel="stylesheet" href="/static/lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <!--表格头部添加栏-->
        <script type="text/html" id="toolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal data-add-btn" lay-event="add">添加</button>
                <button class="layui-btn layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
            </div>
        </script>
        <!--数据表格主体-->
        <table id="dept-table" lay-filter="dept-table"></table>
        <!--右侧修改授权删除-->
        <script type="text/html" id="auth-state">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
    </div>
</div>
<script src="/static/lib/layui/layui.js" charset="utf-8"></script>
<script>
    layui.extend({
        treeTable: '/static/js/lay-module/treetable-lay/treeTable' //table树形扩展
    }).use(['treeTable', 'form', 'table'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var treeTable = layui.treeTable;
        // 渲染表格
        var tableId = treeTable.render({
            elem: '#dept-table',
            url: '/sys/dept/list',
            toolbar: '#toolbar',
            defaultToolbar: ['filter', 'exports', 'print'],
            tree: {
                iconIndex: 1,           // 折叠图标显示在第几列
                isPidData: true,        // 是否是id、pid形式数据
                idName: 'id',  // id字段名称
                pidName: 'parentId',     // pid字段名称
                getIcon: function (obj) {
                    return '';
                }
            },
            cols: [
                [
                    {type: "checkbox", width: '5%'},
                    {field: 'name', width: '30%', title: '部门名称'},
                    {field: 'empName', width: '20%', title: '部门负责人', align: "center"},
                    {field: 'empTelephone', width: '20%', title: '负责人电话', align: "center"},
                    {field: 'description', width: '20%', title: '描述', align: "center"},
                    {field: 'createDate', width: '20%', title: '创建时间', sort: true, align: "center"},
                    {title: '操作', width: '30%', toolbar: '#auth-state', align: "center"}
                ]
            ],
            id: 'tableId',
            skin: 'line'
        });

        //执行重载
        var active = {
            reloads: function () {
                //执行重载
                tableId.reload({
                }, 'data');
            }
        };

        /**
         * toolbar监听事件（新增和批量删除）
         */
        treeTable.on('toolbar(dept-table)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var open = layer.open({
                    title: '添加部门',
                    type: 2,
                    shadeClose: true,
                    scrollbar: false,
                    area: ['60%', '80%'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    content: '/sys/dept/show',
                    yes: function (index, layero) {
                        var body = layer.getChildFrame('body', index);
                        //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.layui.form.on("submit(save)", function (data) {
                            var files = data.field;
                            $.ajax({
                                type: "post",
                                url: "/sys/dept/save",
                                dataType: "json",
                                data: files,
                                success: function (data) {
                                    layer.closeAll();
                                    layer.msg(data.msg);
                                    active.reloads();
                                },
                                error: function (data) {
                                    layer.closeAll();
                                    layer.msg(data);
                                }
                            });
                            return false;
                        });
                        body.find("#save").click();
                    }
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var data = tableId.checkStatus();
                if (data.length <= 0) {
                    layer.msg("未选中任何行");
                    return;
                } else {
                    var ids =[];
                    ids[0] = data[0].id;
                    var names = data[0].name;
                    for (var i = 1; i < data.length; i++) {
                        ids[i] = data[i].id;
                        names += "," + data[i].name;
                    }
                    layer.confirm('确认删除:' + names, function (index) {
                        $.ajax({
                            type: "delete",
                            url: "/sys/dept/delete",
                            dataType: "json",
                            traditional: true,
                            data: {"ids": ids},
                            success: function (data) {
                                layer.closeAll();
                                layer.msg(data.msg);
                                active.reloads();
                            },
                            error: function (data) {
                                layer.closeAll();
                                layer.msg("错误！请联系管理员");
                            }
                        });
                        layer.close(index);
                    });
                }
            }
        });

        //状态开关
        treeTable.on('switch(status)', function (obj) {
            $.ajax({
                type: "put",
                url: "/sys/dept/edit",
                dataType: "json",
                data: {
                    "id": obj.value,
                    "status": obj.elem.checked ? "1" : "-1"
                },
                success: function (data) {
                    layer.closeAll();
                },
                error: function (data) {
                    layer.closeAll();
                    layer.msg("错误！请联系管理员");
                }
            });
        });

        //操作监听(修改、删除单个、授权)
        treeTable.on('tool(dept-table)', function (obj) {
            var data = obj.data;
            //修改部门
            if (obj.event === 'edit') {
                var open = layer.open({
                    title: '修改部门信息',
                    type: 2,
                    shadeClose: true,
                    scrollbar: false,
                    area: ['60%', '80%'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    content: '/sys/dept/show?id='+data.id,
                    yes: function (index, layero) {
                        var body = layer.getChildFrame('body', index);
                        //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.layui.form.on("submit(save)", function (data1) {
                            var files = data1.field;
                            files['id'] = data.id;
                            $.ajax({
                                type: "put",
                                url: "/sys/dept/edit",
                                dataType: "json",
                                data: files,
                                success: function (data) {
                                    layer.closeAll();
                                    layer.msg(data.msg);
                                    active.reloads();
                                },
                                error: function (data) {
                                    layer.closeAll();
                                    layer.msg("错误！请联系管理员");
                                }
                            });
                            return false;
                        });
                        body.find("#save").click();
                    }
                });
                return false;
            } else if (obj.event === 'delete') {        //删除单个部门
                layer.confirm('确认要删除:' + data.name, function (index) {
                    $.ajax({
                        type: "delete",
                        url: "/sys/dept/delete",
                        dataType: "json",
                        data: {
                            "ids": data.id
                        },
                        success: function (data) {
                            layer.closeAll();
                            layer.msg(data.msg);
                            if (data.success) {
                                obj.del();
                            }
                        },
                        error: function (data) {
                            layer.closeAll();
                            layer.msg("错误！请联系管理员");
                        }
                    });
                    layer.close(index);
                });
            }
        });

        //加载自定义方法
        $('[data-type]').click(function () {
            var type = $(this).data('type');
            active[type].call(this);
        })
    });
</script>
</body>
</html>